package com.jinyi.wwdeng.ui.config.act

import android.content.Intent
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.view.Gravity
import android.view.View
import androidx.core.content.ContextCompat
import com.jinyi.base.base.BaseActivity
import com.jinyi.base.utils.ActivityStackManager
import com.jinyi.base.utils.BarUtils
import com.jinyi.base.utils.HHLog
import com.jinyi.wwdeng.R
import com.jinyi.wwdeng.databinding.ActIdcardCropBinding
import java.io.File
import java.io.FileOutputStream
import java.io.IOException

/**
 * JinYiTest
 * @Author wwdeng
 * @CreateTime: 2024/4/22 15:50
 * @Annotation：身份证剪裁
 */
class IdCardCropAct: BaseActivity() {

    lateinit var binding: ActIdcardCropBinding

    private var outputFile: File? = null

    companion object {
        const val KEY_FILE_PATH = "filePath"//文件路径
        const val KEY_CONTENT_TYPE = "contentType"//1相机  2相册
    }

    override fun getRootView(): View {
        binding = ActIdcardCropBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initView() {
        setTitleBarVisibility(View.VISIBLE)
        setTitleBarColor(ContextCompat.getColor(this, R.color.color_353535))
        setBackBtnRes(R.mipmap.pic_back_white_icon)
        setRightBtnRes(R.mipmap.pic_kefu_white_icon)
        setTextTitle(getString(R.string.confirmer_le_compte))
        setTextTitleColor(ContextCompat.getColor(this, com.jinyi.lib_base.R.color.color_ffffff))
        setTextTitleGravity(Gravity.CENTER)
        BarUtils.setStatusBarLightMode(this, false)
    }

    override fun initListener() {
        //返回
        binding.btnCancel.setOnClickListener {
            finish()
        }
        //旋转
        binding.btnRotate.setOnClickListener {
            binding.cropView.rotate(90)
        }
        //确认
        binding.btnConfirm.setOnClickListener {
            doConfirmResult()
        }
    }

    override fun initData() {
        val bundle = intent.extras
        val outputPath = bundle!!.getString(KEY_FILE_PATH)
        HHLog.e("接收到的文件路径>>>>>>>>>>>>>>>>>>$outputPath")
        binding.cropView.setFilePath(outputPath)

        //1相机 2相册
        //相机outputFile是直接修改原图片   相册outputFile需要创建新文件
        val contentType = bundle!!.getInt(KEY_CONTENT_TYPE)
        if (contentType == 1 && outputPath != null){
            outputFile = File(outputPath)
        }
    }

    /**
     * 确认
     */
    private fun doConfirmResult() {
        if (outputFile == null) {
            val filePath = Environment.getExternalStorageDirectory().toString() + File.separator + "DCIM/Camera/";
            outputFile = File(filePath,"${System.currentTimeMillis()}.jpg")
        }
        Thread{
            try {
                val rect = binding.cropMaskView.frameRect
                val croppedBitmap = binding.cropView.crop(rect)
                val fileOutputStream = FileOutputStream(outputFile)
//                val bitmap = (displayImageView.getDrawable() as BitmapDrawable).bitmap
                croppedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream)
                fileOutputStream.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
            //刷新图库
            sendBroadcast(Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE", Uri.fromFile(outputFile)))
            //需要关闭相机页面
            ActivityStackManager.finishActivity(IdCardCameraAct::class.java)

            val bundle = Bundle()
            bundle.putString(KEY_FILE_PATH,outputFile!!.absolutePath)
            onIntent(UploadAct::class.java,bundle)
            finish()
        }.start()
    }
}